﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.AI;

namespace UFrame
{
    public class NavRect : MonoBehaviour
    {
        public int id = -1;
        private BoxCollider2D box;
        private NavMeshObstacle meshObstacle;
        public Vector3 scale = new Vector3(1, 1, 1);
        public Vector3 startPosition;
        public Vector3 endPosition;
        private Vector3 drawJumpIcon = new Vector3(0.3f, 0.3f, 0.3f);
        private Vector3 drawPositionIcon = new Vector3(0.1f, 0.1f, 0.1f);

        [SerializeField] public List<JumpPoint> borderUponPoint = new List<JumpPoint>();

        public Vector3 StartPosition
        {
            get { return new Vector2(transform.position.x - box.size.x / 2, transform.position.y + box.size.y); }
        }

        public Vector3 EndPosition
        {
            get { return new Vector2(transform.position.x + box.size.x / 2, transform.position.y + box.size.y); }
        }

        public Vector3 EndHeightPosition
        {
            get { return new Vector2(transform.position.x, transform.position.y + (box.size.y / 2 + box.size.y)); }
        }

        public Vector3 StartHeightPosition
        {
            get
            {
                return new Vector2(transform.position.x, transform.position.y - box.size.y / 2);
                //return new Vector2(transform.position.x, transform.position.y + box.size.y / 2);
            }
        }

        public List<JumpPoint> BorderUponPoint
        {
            get
            {
                List<JumpPoint> newList = new List<JumpPoint>();

                foreach (var item in borderUponPoint)
                {
                    if (item.target == null)
                    {
                        continue;
                    }

                    JumpPoint jp = item.Clone() as JumpPoint;
                    newList.Add(jp);
                    jp.startPosition = transform.position + jp.startPosition;
                    jp.endPosition = jp.target.transform.position + jp.endPosition;
                }

                return newList;
            }
        }


        private void Awake()
        {
            if (meshObstacle == null)
            {
                meshObstacle = GetComponent<NavMeshObstacle>();
            }

            if (box == null)
            {
                box = GetComponent<BoxCollider2D>();
            }
        }

        // Start is called before the first frame update
        void Start()
        {
        }

        // Update is called once per frame
        void Update()
        {
        }


        public NavRect GetNavRect()
        {
            return this;
        }
#if UNITY_EDITOR


        void OnDrawGizmos()
        {
            if (meshObstacle == null)
            {
                meshObstacle = GetComponent<NavMeshObstacle>();
            }

            if (box == null)
            {
                box = GetComponent<BoxCollider2D>();
            }
            else
            {
                Gizmos.color = Color.blue;
                Gizmos.DrawWireCube(new Vector2(transform.position.x, transform.position.y + box.size.y),
                    box.size * transform.localScale * scale);

                //绘制开始和结束点，开始高度和结束高度

                //float width = box.size.x;
                //float halfWidth = box.size.x / 2;
                //float height = box.size.y;
                //float halfHeight = box.size.y / 2;

                Gizmos.color = Color.yellow;
                Gizmos.DrawWireCube(StartPosition, drawPositionIcon);
                Gizmos.DrawWireCube(EndPosition, drawPositionIcon);
                Gizmos.DrawWireCube(StartHeightPosition, drawPositionIcon);
                Gizmos.DrawWireCube(EndHeightPosition, drawPositionIcon);
                Handles.Label(StartPosition, StartPosition.ToString());
                Handles.Label(EndPosition, EndPosition.ToString());
                Handles.Label(StartHeightPosition, StartHeightPosition.ToString());
                Handles.Label(EndHeightPosition, EndHeightPosition.ToString());

                Vector3 idPos = StartHeightPosition;
                idPos.y += 1;
                Handles.Label(idPos, "平台ID:" + id.ToString());
            }


            if (borderUponPoint != null)
            {
                foreach (var item in borderUponPoint)
                {
                    if (item.target == null)
                    {
                        continue;
                    }

                    Vector2 circle = new Vector2(transform.position.x + item.startPosition.x,
                        transform.position.y + item.startPosition.y);
                    Vector2 jumpEndPosition = new Vector2(item.target.transform.position.x + item.endPosition.x,
                        item.target.transform.position.y + item.endPosition.y);


                    Gizmos.DrawWireCube(circle, drawJumpIcon);
                    Handles.Label(circle, circle.ToString());
                    Gizmos.DrawLine(circle, jumpEndPosition);
                    Handles.Label(jumpEndPosition, jumpEndPosition.ToString());
                    Gizmos.DrawWireCube(jumpEndPosition, drawJumpIcon);
                }
            }
        }

#endif
    }

    [Serializable]
    public class JumpPoint : ICloneable
    {
        public Vector3 startPosition;
        public Vector3 endPosition;
        public NavRect target;

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }
}